﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System.Linq;
using DevExpress.Utils;

namespace DevEx.QuocSy.UserControl
{
    public partial class ucSanPham : DevExpress.XtraEditors.XtraUserControl
    {
        public ucSanPham()
        {
            InitializeComponent();
        }

        private void ucSanPham_Load(object sender, EventArgs e)
        {
            KhoiTaodtgLoaiSanPham("");
            KhoiTaoGiaoDienLoaiSanPham(true);
            KhoiTaoGiaoDienSanPham(true);
            gridView1.OptionsDetail.ShowDetailTabs = false;             
        }
        
        public void KhoiTaodtgLoaiSanPham(String Sname)
        {
            //CLoaiSanPham LoaiSanPham = new CLoaiSanPham();
            //dtgLoaiSanPham.DataSource = LoaiSanPham.SelectAllRecord();            
            QLVTDataContext dc = new QLVTDataContext();
            var q = from p in dc.tblSanPhams where p.ID_Parent == null
                    select new { p.ID_SanPham ,p.SanPham, p.ID_Parent, p.KH_SanPham };
            dtgLoaiSanPham.DataSource = q;

            gridView1.Columns.ColumnByFieldName("ID_Parent").Visible = false;
            gridView1.Columns.ColumnByFieldName("ID_SanPham").Visible = false;

            //focus
            int focusrow = 0;
            if (Sname != "")
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    // if (dtcustomer.Rows[i]["CUST_NAME"].ToString().ToUpper() == Sname.ToUpper())
                    if (gridView1.GetRowCellValue(i, "SanPham").ToString().ToUpper() == Sname.ToUpper())
                    {
                        focusrow = i;
                    }
                }
            }

            gridView1.FocusedRowHandle = focusrow;

            //Columns LoaiSanPham
            var cl_LoaiSanPham = gridView1.Columns.ColumnByFieldName("SanPham");
            //cl_LoaiSanPham.Width = 30;
            cl_LoaiSanPham.Caption = "Đầu việc";
            cl_LoaiSanPham.OptionsColumn.AllowEdit = false;
            cl_LoaiSanPham.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_LoaiSanPham.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns KH
            var cl_KyHieu = gridView1.Columns.ColumnByFieldName("KH_SanPham");
            //cl_LoaiSanPham.Width = 30;
            cl_KyHieu.Caption = "Ký hiệu";
            cl_KyHieu.OptionsColumn.AllowEdit = false;
            cl_KyHieu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_KyHieu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
        }

        Boolean fThem = false;
        Boolean fSua = false;
        CLoaiSanPham m_LoaiSanPham = new CLoaiSanPham();

        private void btnThem_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDienLoaiSanPham(false);
            txtLoaiSanPham.EditValue = "";
            txtKH.EditValue = "";
            txtLoaiSanPham.Focus();
            fThem = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDienLoaiSanPham(false);
            txtLoaiSanPham.Focus();
            fSua = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int ID_Parent = 0;
            int ID_SanPham;
            ID_Parent = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_Parent"));
            ID_SanPham = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_SanPham"));
            String SanPham = Convert.ToString(gridView1.GetFocusedRowCellValue("SanPham"));

            QLVTDataContext dc = new QLVTDataContext();
            var tt_LoaiSanPham = (from p in dc.tblSanPhams where p.ID_Parent == ID_Parent select p).Count();

            if (tt_LoaiSanPham > 0)
            {
                MessageBox.Show("Đầu việc: " + SanPham + " đang sử dụng, không xóa được.", "Chú ý");
                return;
            }       

            if (MessageBox.Show("Bạn muốn xóa Đầu việc: "+ SanPham + "?", "Chú ý", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                m_SanPham.Delete(ID_SanPham);
                KhoiTaodtgLoaiSanPham("");
            }
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            if (fThem)
            {
                tblSanPham DauViec = new tblSanPham();
                DauViec.SanPham = txtLoaiSanPham.Text;
                DauViec.ID_Parent = null;
                DauViec.KH_SanPham = Convert.ToString(txtKH.EditValue);
                m_SanPham.Insert(DauViec);
            }

            if (fSua)
            {
                tblSanPham DauViec = new tblSanPham();
                DauViec.SanPham = txtLoaiSanPham.Text;
                DauViec.ID_Parent = null;
                DauViec.KH_SanPham = Convert.ToString(txtKH.EditValue);
                DauViec.ID_SanPham = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_SanPham"));
                m_SanPham.Update(DauViec);                               
            }

            KhoiTaoGiaoDienLoaiSanPham(true);
            KhoiTaodtgLoaiSanPham(txtLoaiSanPham.Text);
            fThem = false;
            fSua = false;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDienLoaiSanPham(true);
            fThem = false;
            fSua = false;
            String SanPham = Convert.ToString(gridView1.GetFocusedRowCellValue("SanPham"));
            KhoiTaodtgLoaiSanPham(SanPham);
        }

        public void KhoiTaoGiaoDienLoaiSanPham(Boolean f)
        {
            btnThem.Visible = f;
            btnSua.Visible = f;
            btnXoa.Visible = f;
            btnGhi.Visible = !f;
            btnHuy.Visible = !f;
            txtLoaiSanPham.Properties.ReadOnly = f;
            txtKH.Properties.ReadOnly = f;
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            txtLoaiSanPham.EditValue = gridView1.GetFocusedRowCellValue("SanPham");
            txtKH.EditValue = gridView1.GetFocusedRowCellValue("KH_SanPham");
            int ID_SanPham = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_SanPham"));
            KhoiTaodtgSanPham(ID_SanPham, "");

            txtSanPham.EditValue = gridView2.GetFocusedRowCellValue("SanPham");
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (Convert.ToInt32(e.RowHandle) + 1).ToString();
                //if (!indicatorIcon)
                e.Info.ImageIndex = -1;
            }
        }
        
        //San Pham
        public void KhoiTaodtgSanPham(int ID_Parent, String Sname)
        {
            //CSanPham SanPham = new CSanPham();
            //dtgSanPham.DataSource = SanPham.SelectSanPhamByLoaiSanPham(ID_Parent);

            QLVTDataContext dc = new QLVTDataContext();
            var q = from p in dc.tblSanPhams where p.ID_Parent == ID_Parent 
                    select new { p.ID_Parent, p.ID_SanPham ,p.SanPham  };
            dtgSanPham.DataSource = q;

            gridView2.Columns.ColumnByFieldName("ID_SanPham").Visible = false;
            gridView2.Columns.ColumnByFieldName("ID_Parent").Visible = false;
            //gridView2.Columns.ColumnByFieldName("tblLoaiSanPham").Visible = false;

            //focus
            int focusrow = 0;
            if (Sname != "")
            {
                for (int i = 0; i < gridView2.RowCount; i++)
                {
                    // if (dtcustomer.Rows[i]["CUST_NAME"].ToString().ToUpper() == Sname.ToUpper())
                    if (gridView2.GetRowCellValue(i, "SanPham").ToString().ToUpper() == Sname.ToUpper())
                    {
                        focusrow = i;
                    }
                }
            }
            gridView2.FocusedRowHandle = focusrow;

            //Columns SanPham
            var cl_SanPham = gridView2.Columns.ColumnByFieldName("SanPham");
            //cl_LoaiSanPham.Width = 30;
            cl_SanPham.Caption = "Sản phẩm";
            cl_SanPham.OptionsColumn.AllowEdit = false;
            cl_SanPham.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_SanPham.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
        }

        Boolean fThemSP = false;
        Boolean fSuaSP = false;
        CSanPham m_SanPham = new CSanPham();

        private void btnThemSP_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDienSanPham(false);
            txtSanPham.EditValue = "";
            txtSanPham.Focus();
            fThemSP = true;
        }

        private void btnSuaSP_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDienSanPham(false);
            txtSanPham.Focus();
            fSuaSP = true;
        }

        private void btnXoaSP_Click(object sender, EventArgs e)
        {
            int ID_SanPham = 0;
            ID_SanPham = Convert.ToInt32(gridView2.GetFocusedRowCellValue("ID_SanPham"));
            String SanPham = Convert.ToString(gridView2.GetFocusedRowCellValue("SanPham"));

            QLVTDataContext dc = new QLVTDataContext();
            var tt_SanPham = (from p in dc.tblDPNXes where p.ID_SanPham == ID_SanPham select p).Count();

            if (tt_SanPham > 0)
            {
                MessageBox.Show("Sản phẩm: " + SanPham + " đang sử dụng, không xóa được.", "Chú ý");
                return;
            }         

            if (MessageBox.Show("Bạn muốn xóa Sản phẩm: " + SanPham + "?", "Chú ý", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                
                m_SanPham.Delete(ID_SanPham);
                int ID_Parent = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_Parent"));
                KhoiTaodtgSanPham(ID_Parent,"");
            }
        }

        private void btnGhiSP_Click(object sender, EventArgs e)
        {
            int ID_Parent = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_SanPham"));
            if (fThemSP)
            {
                tblSanPham SanPham = new tblSanPham();
                SanPham.SanPham = txtSanPham.Text;
                SanPham.KH_SanPham = "";
                SanPham.ID_Parent = ID_Parent;
                m_SanPham.Insert(SanPham);
            }

            if (fSuaSP)
            {
                tblSanPham SanPham = new tblSanPham();
                SanPham.ID_Parent = ID_Parent;
                SanPham.ID_SanPham = Convert.ToInt32(gridView2.GetFocusedRowCellValue("ID_SanPham"));
                SanPham.SanPham = txtSanPham.Text;
                SanPham.KH_SanPham = "";
                m_SanPham.Update(SanPham);
            }

            KhoiTaoGiaoDienSanPham(true);
            KhoiTaodtgSanPham(ID_Parent,txtSanPham.Text);
            fThemSP = false;
            fSuaSP = false;
        }

        private void btnHuySP_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDienSanPham(true);
            fThemSP = false;
            fSuaSP = false;
        }

        public void KhoiTaoGiaoDienSanPham(Boolean f)
        {
            btnThemSP.Visible = f;
            btnSuaSP.Visible = f;
            btnXoaSP.Visible = f;
            btnGhiSP.Visible = !f;
            btnHuySP.Visible = !f;
            txtSanPham.Properties.ReadOnly = f;
        }

        private void gridView2_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (Convert.ToInt32(e.RowHandle) + 1).ToString();
                //if (!indicatorIcon)
                e.Info.ImageIndex = -1;
            }
        }

        private void gridView2_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            txtSanPham.EditValue = gridView2.GetFocusedRowCellValue("SanPham");
        }

        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            // Khoá dtg khi thêm hoặc sửa
            if (btnThem.Visible == false || btnThemSP.Visible == false)
                (e as DXMouseEventArgs).Handled = true;
            else
                (e as DXMouseEventArgs).Handled = false;
        }

        private void gridView2_MouseDown(object sender, MouseEventArgs e)
        {
            // Khoá dtg khi thêm hoặc sửa
            if (btnThem.Visible == false || btnThemSP.Visible == false)
                (e as DXMouseEventArgs).Handled = true;
            else
                (e as DXMouseEventArgs).Handled = false;
        }


        
    }
}
